package strcy.cn.market.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import strcy.cn.market.mapper.UserMapper;
import strcy.cn.market.pojo.User;
import strcy.cn.market.service.UserService;
import strcy.cn.market.utils.BCrypt;
import strcy.cn.market.utils.JwtUtil;
import strcy.cn.market.utils.ThreadLocalUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public User getByEmail(String email) {
        User u = userMapper.getByEmail(email);
        return u;
    }

    @Override
    public void addUser(String username, String password, String email) {
        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        //BCrypt加密
        String pwd = BCrypt.encryptPassword(password);
        user.setPassword(pwd);
        //user角色默认为用户
        user.setRole(0);
        //user默认状态status为1
        user.setState(1);
        userMapper.add(user);
    }

    @Override
    public String login(User u) {
        //创建claims
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", u.getUserId());
        claims.put("username", u.getUsername());
        claims.put("email", u.getEmail());
        //生成jWT令牌
        String token = JwtUtil.genToken(claims);
        //将token存入redis中
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set(token,token,1, TimeUnit.HOURS);
        return token;
    }

    @Override
    public List<User> getUserAll() {
        List<User> users = userMapper.getUserAll();
        return users;
    }

    @Override
    public User getUserById(Integer id) {
        User user = userMapper.getUserById(id);
        return user;
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }


    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        //获取用户id
        Map<String,Object> map =  ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPassword) {
        String password = BCrypt.encryptPassword(newPassword);
        //获取用户id
        Map<String,Object> map =  ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(password,id);
    }

    @Override
    public void updateStatusById(Integer id ,String status) {
        userMapper.updateStatusById(id,status);
    }

    @Override
    public void addUser(User user) {
        //BCrypt加密
        String pwd = BCrypt.encryptPassword(user.getPassword());
        user.setPassword(pwd);
        userMapper.add(user);
    }

}
